//
//  ByCellOrderDetailFirst.m
//  oc_knowledge
//
//  Created by mc on 17/1/20.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellOrderDetailFirst.h"
#define BGCOLOR [UIColor colorWithR:252 g:126 b:35]
@implementation ByLayoutOrderDetailFirst
-(void)setModel:(ByModelOrderDetail *)model{
    _model = model;
    
    //headerView
    CGFloat x,y,width,height;
    x = y = 0;
    width = SCREEN_WIDTH;
    height = 85;
    _headerViewFrame = CGRectMake(x, y, width, height);
    
    //如果有物流信息
    if (model.logisticInfo.length != 0) {
        //物流icon
        x = 10;
        width = height = 30;
        _logisticIconFrame = CGRectMake(x, 0, width, height);
        
        //物流信息
        x = CGRectGetMaxX(_logisticIconFrame) + 10;
        y = CGRectGetMaxY(_headerViewFrame) + 10;
        width = SCREEN_WIDTH - x - 10 - 20 - 10;
        height = [_model.logisticInfo getRectOfStringWithSize:CGSizeMake(width, MAXFLOAT) font:14].size.height;
        _logisticInfoFrame = CGRectMake(x, y, width, height);
        
        //物流时间
        y = CGRectGetMaxY(_logisticInfoFrame) + 5;
        height = 20;
        _logisticTimeFrame = CGRectMake(x, y, width, height);
        
        _logisticIconFrame.origin.y = CGRectGetMaxY(_headerViewFrame) + (CGRectGetHeight(_logisticInfoFrame) + CGRectGetHeight(_logisticTimeFrame) + 20)/2 - 15;
        
        //箭头
        x = SCREEN_WIDTH - 10 - 20;
        y = _logisticIconFrame.origin.y;
        width = height = 30;
        _arrowFrame = CGRectMake(x, y, width, height);
        
        //线
        x = 10;
        y = CGRectGetMaxY(_logisticTimeFrame) + 10;
        width = SCREEN_WIDTH - 2*x;
        height = 0.5;
        _lineFrame = CGRectMake(x, y, width, height);

    }else{
        y = CGRectGetMaxY(_headerViewFrame);
        _logisticIconFrame = CGRectMake(0, y, 0, 0);
        _logisticInfoFrame = CGRectMake(0, y, 0, 0);
        _logisticTimeFrame = CGRectMake(0, y, 0, 0);
        _lineFrame = CGRectMake(0, y, 0, 0);
    }
    
    //地址icon
    x = 10;
    width = height = 30;
    _addressIconFrame = CGRectMake(x, 0, width, height);
 
    ByModelOrderDetailAddress *address_model = _model.address;
    //电话
    y = CGRectGetMaxY(_lineFrame) + 10;
    width = [address_model.mobile getSizeOfString:14].width + 10;
    x = SCREEN_WIDTH - 10 - width;
    height = 20;
    _mobileFrame =CGRectMake(x, y, width, height);
    
    //收货人
    x = CGRectGetMaxX(_addressIconFrame) + 10;
    width = SCREEN_WIDTH - x - width - 20;
    _contactnameFrame = CGRectMake(x, y, width, height);
    
    //收货地址
    NSString *str;
    if ([address_model.city isEqualToString:@"市辖区"] || [address_model.city isEqualToString:@"县"]) {
        str = [NSString stringWithFormat:@"收货地址：%@%@%@",address_model.province,address_model.area,address_model.detailinfo];
    }else{
        str = [NSString stringWithFormat:@"收货地址：%@%@%@%@",address_model.province,address_model.city,address_model.area,address_model.detailinfo];
    }
    y = CGRectGetMaxY(_contactnameFrame) + 5;
    width = SCREEN_WIDTH - x - 10;
    height = [str getRectOfStringWithSize:CGSizeMake(width, MAXFLOAT) font:14].size.height;
    _addressDetailFrame = CGRectMake(x, y, width, height);
    
    //地址icon
    _addressIconFrame.origin.y = (10 + 20 + 5 + height + 10)/2 - 15 + CGRectGetMaxY(_lineFrame);
    
    //cellFrame
    x = y = 0;
    width = SCREEN_WIDTH;
    height = CGRectGetMaxY(_addressDetailFrame) + 10;
    _cellFrame = CGRectMake(x, y, width, height);
    
}
@end


//----------------------------------------------------------Cell----------------------------------------------------/
@interface ByCellOrderDetailFirst()
{
    UIView       *_headerView;               //头视图
    UIImageView  *_logisticIcon;             //物流信息图片
    UILabel      *_logisticInfoLabel;        //物流信息
    UILabel      *_logisticTimeLabel;        //物流时间
    UIImageView  *_arrowIcon;                //箭头
    UIView       *_lineView;                 //线
    UIImageView  *_addressIcon;
    UILabel      *_mobileLabel;
    UILabel      *_contactnameLabel;
    UILabel      *_addressDetailLabel;

}
@end

@implementation ByCellOrderDetailFirst
+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellOrderDetailFirst";
    ByCellOrderDetailFirst *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        [self setViews];
    }
    return self;
}
#pragma mark --创建视图
- (void)setViews{
    _headerView = [[UIView alloc] init];
    _headerView.backgroundColor = BGCOLOR;
    [self addSubview:_headerView];
    
    _logisticIcon = [[UIImageView alloc] init];
    _logisticIcon.backgroundColor = BGCOLOR;
    [self addSubview:_logisticIcon];
    
    _logisticInfoLabel = [[UILabel alloc] init];
    _logisticInfoLabel.numberOfLines = 0;
    _logisticInfoLabel.font = FONT(14);
    [self addSubview:_logisticInfoLabel];
    
    _logisticTimeLabel = [[UILabel alloc] init];
    _logisticTimeLabel.font = FONT(12);
    [self addSubview:_logisticTimeLabel];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    [self addSubview:_lineView];
    
    _addressIcon = [[UIImageView alloc] init];
    _addressIcon.backgroundColor = BGCOLOR;
    [self addSubview:_addressIcon];
    
    _mobileLabel = [[UILabel alloc] init];
    _mobileLabel.font = FONT(14);
    _mobileLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_mobileLabel];
    
    _contactnameLabel = [[UILabel alloc] init];
    _contactnameLabel.font = FONT(14);
    [self addSubview:_contactnameLabel];
    
    _addressDetailLabel = [[UILabel alloc] init];
    _addressDetailLabel.numberOfLines = 0;
    _addressDetailLabel.font = FONT(14);
    [self addSubview:_addressDetailLabel];

}

-(void)setLayout:(ByLayoutOrderDetailFirst *)layout{
    _layout = layout;
    //设置Frame
    _headerView.frame               =  _layout.headerViewFrame;
    _logisticIcon.frame             = _layout.logisticIconFrame;
    _logisticInfoLabel.frame        = _layout.logisticInfoFrame;
    _logisticTimeLabel.frame        = _layout.logisticTimeFrame;
    _arrowIcon.frame                = _layout.arrowFrame;
    
    _lineView.frame                 = _layout.lineFrame;
    _addressIcon.frame              = _layout.addressIconFrame;
    _contactnameLabel.frame         = _layout.contactnameFrame;
    _mobileLabel.frame              = _layout.mobileFrame;
    _addressDetailLabel.frame       = _layout.addressDetailFrame;
    
    //设置model
    ByModelOrderDetail *model = _layout.model;
    _mobileLabel.text = model.address.mobile;

    _contactnameLabel.text = [NSString stringWithFormat:@"收货人：%@",model.address.contactname];
    NSString *detailAddress;
    if ([model.address.city isEqualToString:@"市辖区"] || [model.address.city isEqualToString:@"县"]) {
        detailAddress = [NSString stringWithFormat:@"收货地址：%@%@%@",model.address.province,model.address.area,model.address.detailinfo];
    }else{
        detailAddress = [NSString stringWithFormat:@"收货地址：%@%@%@%@",model.address.province,model.address.city,model.address.area,model.address.detailinfo];

    }
    _addressDetailLabel.text = detailAddress;
    
    
    [self createStatusView:model];

    
}
#pragma mark ---
- (void)createStatusView:(ByModelOrderDetail *)model{
    for (UIView *subview in _headerView.subviews) {
        [subview removeFromSuperview];
    }
    
    CGFloat x,y,width,height;
    //图片
    NSString *imgStr,*titleStr;
    switch(model.query_status.integerValue){
        case 1: //待付款
        {
            imgStr = @"待付款";
            titleStr = @"等待买家付款";
        }
            break;
        case 2: //待发货
        {
            imgStr = @"待发货";
            titleStr = @"等待卖家发货";
        }
            break;
        case 3: //待收货
        {
            imgStr = @"待收货";
            titleStr = @"等待买家收货";
        }
            break;
        case 4: //待评价
        {
            imgStr = @"待评价";
            titleStr = @"等待买家评价";
        }
            break;
    }
    x = SCREEN_WIDTH - 120 - 10;
    y = 12.5;
    width = 120;
    height = 60;
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [_headerView addSubview:img];
    [img setImage:[UIImage imageNamed:imgStr]];
    img.backgroundColor = [UIColor yellowColor];
    
    //订单状态
    x = 50;
    width = SCREEN_WIDTH - width - 10 - x - 10;
    //待付款状态
    if (model.query_status.integerValue == 1) {
        y = 20;
    }
    //其他
    else{
        y = 32.5;
    }
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, 20)];
    titleLabel.text = titleStr;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = FONT(14);
    [_headerView addSubview:titleLabel];
    
    y = titleLabel.bottom + 10;
    if (model.query_status.integerValue == 1){
        height = 15;
    }else{
        height = 0;
    }
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.font = FONT(14);
    [_headerView addSubview:timeLabel];
    

}

@end
